import { Node, TweenEasing, tween, v2, v3 } from "cc";
import JNLayerBase from "../ui/base/JNLayerBase";

export interface AnimsNodeInfo{
    key:string,
    start:any,
    end:any,
}

//CC Node 动画工具
export default class AnimsNodeUtil{
    
    static onEnlarge(node:Node,time:number = 0.3,info:AnimsNodeInfo = {key:"scale",start:v3(0,0,0),end:v3(1,1,1)},callback?:Function){
        console.log("AnimsNodeUtil_onEnlarge",node);
        node[info.key] = info.start;
        tween(node)
            .to(time,{[info.key]: info.end},{
                onComplete(){
                    callback && callback();
                },
            })
            .start();
    }

    static onSmaller(node:Node,time:number = 0.3,info:AnimsNodeInfo = {key:"scale",start:v3(0,0,0),end:v3(0,0,0)},callback?:Function){
        console.log("AnimsNodeUtil_onSmaller",node);
        tween(node)
            .to(time,{[info.key]:info.end},{
                onComplete(){
                    callback && callback();
                },
            })
            .start();
    }

    static onCubicInOutOpen(node:Node,time:number = 0.5,info?:AnimsNodeInfo){
        this.Open("cubicInOut",node,time,info);
    }

    static onCubicInOutClose(node:Node,time:number = 0.5,info?:AnimsNodeInfo){
        this.Close("cubicInOut",node,time,info);
    }

    static onBackOutOpen(node:Node,time:number = 0.5,info?:AnimsNodeInfo,callback?:Function){
        this.Open("backOut",node,time,info,callback);
    }

    static onBackInClose(node:Node,time:number = 0.5,info?:AnimsNodeInfo,callback?:Function){
        this.Close("backIn",node,time,info,callback);
    }

    static Open(easing:TweenEasing,node:Node,time:number = 0.3,info:AnimsNodeInfo = {key:"scale",start:v3(0,0,0),end:v3(1,1,1)},callback?:Function){
        // console.log("AnimsNodeUtil_Open",node);
        node[info.key] = info.start;
        tween(node)
            .to(time,{[info.key]: info.end},{
                onComplete(){
                    callback && callback();
                },
                easing
            })
            .start();
    }
    static Close(easing:TweenEasing,node:Node,time:number = 0.3,info:AnimsNodeInfo = {key:"scale",start:v3(0,0,0),end:v3(0,0,0)},callback?:Function){
        // console.log("AnimsNodeUtil_Close",node);
        tween(node)
            .to(time,{[info.key]:info.end},{
                onComplete(){
                    callback && callback();
                },
                easing
            })
            .start();
    }
}
